package pl.kemot.app;

import pl.kemot.customer.add.IPresenterCustomerAdd;
import pl.kemot.customer.all.IPresenterCustomerAll;
import pl.kemot.customer.edit.IPresenterCustomerEdit;
import pl.kemot.impl.PresenterComponentRemoveImpl;
import pl.kemot.impl.customer.add.PresenterCustomerAddImpl;
import pl.kemot.impl.customer.add.ViewCustomerAddImpl;
import pl.kemot.impl.customer.all.PresenterCustomerAllImpl;
import pl.kemot.impl.customer.all.ViewCustomerAllImpl;
import pl.kemot.impl.customer.edit.PresenterCustomerEditImpl;
import pl.kemot.impl.customer.edit.ViewCustomerEditImpl;
import pl.kemot.model.CustomerBO;
import pl.kemot.model.CustomerDTO;

import com.vaadin.Application;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class MyVaadinApplication extends Application {

	private Window window;
	private static CustomerBO customerBO = new CustomerBO();
	private IPresenterCustomerAdd presenterCustomerAdd;
	private IPresenterCustomerEdit presenterCustomerEdit;
	private IPresenterCustomerAll presenterCustomerAll;

	private ViewCustomerAllImpl viewCustomerAll;
	private Button butttonCustomerAdd;

	public static CustomerBO getCustomerBO() {
		return customerBO;
	}

	@Override
	public void init() {
		window = new Window("jelonek mvp petstore");
		setMainWindow(window);

		initPresenterCustomerAll();

		initViewCustomerAll();
		initButttonCustomerAdd();

		initPresenterCustomerAdd();
		initPresenterCustomerEdit();

		window.addComponent(viewCustomerAll);
		window.addComponent(butttonCustomerAdd);
	}

	private void initPresenterCustomerAdd() {
		presenterCustomerAdd = new PresenterCustomerAddImpl();
		presenterCustomerAdd.setPresenterComponentRemove(new PresenterComponentRemoveImpl());
		presenterCustomerAdd.setCustomerBO(getCustomerBO());
		presenterCustomerAdd.setViewCustomerAll(viewCustomerAll);
	}

	private void initPresenterCustomerEdit() {
		presenterCustomerEdit = new PresenterCustomerEditImpl();
		presenterCustomerEdit.setPresenterComponentRemove(new PresenterComponentRemoveImpl());
		presenterCustomerEdit.setCustomerBO(getCustomerBO());
		presenterCustomerEdit.setViewCustomerAll(viewCustomerAll);
	}

	private void initPresenterCustomerAll() {
		presenterCustomerAll = new PresenterCustomerAllImpl();
		presenterCustomerAll.setPresenterComponentRemove(new PresenterComponentRemoveImpl());
		presenterCustomerAll.setCustomerBO(getCustomerBO());
	}

	private void initViewCustomerAll() {
		viewCustomerAll = new ViewCustomerAllImpl("Klienci", presenterCustomerAll);
		viewCustomerAll.addListener(new ItemClickListener() {
			public void itemClick(ItemClickEvent event) {
				ViewCustomerEditImpl viewCustomerEditImpl = new ViewCustomerEditImpl("Edytuj klienta", presenterCustomerEdit);
				Long id = (Long) event.getItemId();
				CustomerDTO customerDTO = viewCustomerAll.getCustomerById(id);
				if (customerDTO != null) {
					viewCustomerEditImpl.setId(customerDTO.getId());
					viewCustomerEditImpl.setFirstName(customerDTO.getFirstName());
					viewCustomerEditImpl.setLastName(customerDTO.getLastName());
					viewCustomerEditImpl.setAge(customerDTO.getAge());
				}
				window.addComponent(viewCustomerEditImpl);
			}
		});
		viewCustomerAll.loadData();
	}

	private void initButttonCustomerAdd() {
		butttonCustomerAdd = new Button("Dodaj Klienta");
		butttonCustomerAdd.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				ViewCustomerAddImpl viewCustomerAddImpl = new ViewCustomerAddImpl("Dodaj klienta", presenterCustomerAdd);
				window.addComponent(viewCustomerAddImpl);
			}
		});
	}
}
